package com.lin.blog.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lin.blog.entity.Article;
import com.lin.blog.repository.ArticleCategoryTagsRepository;
import com.lin.blog.service.IArticleCategoryTagsService;
import com.lin.blog.service.IElasticSearchService;
import com.lin.blog.vo.ArticleCategoryTagsVO;
import com.lin.utils.RequestUtils;
import com.lin.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lin
 * @since 2020-03-08
 */
@Api(tags = "Article接口")
@RestController
@RequestMapping("/article")
public class ArticleCategoryTagsController {

    @Autowired
    private IArticleCategoryTagsService articleCategoryTagsService;

    /**
     * https://www.cnblogs.com/ifme/p/12005026.html
     */


    @Autowired
    private IElasticSearchService elasticSearchService;



    /** api 请求 http://localhost:8888/article/search/0/10?title=测试&summary=测试&category.id=2&tags.id=1&tags.id=2&level=0,1&likes=0,10&look=0,10&createTime=2020-03-27 04:38:41,2020-03-28 00:00:00
     *
     * 从es中搜索文章 通过多参数
     * @param searchMap
     * @param pageNum       从0开始
     * @param pageSize
     * @return
     */
    @ApiOperation("从es中搜索文章 通过多参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "第N页"),
            @ApiImplicitParam(name = "pageSize", value = "每页N条")})
    @GetMapping("/search/{pageNum}/{pageSize}")
    public Result searchArticle( @RequestParam Map<String,Object> searchMap,
                                 @PathVariable("pageNum")    Integer pageNum,
                                 @PathVariable("pageSize") Integer pageSize){
        return elasticSearchService.searchArticle(searchMap,pageNum,pageSize);
    }


    /**
     * User 从es中搜索文章 通过关键字
     * @param keyword
     * @param pageNum       从0开始
     * @param pageSize
     * @return
     */
    @ApiOperation("从es中搜索文章 通过关键字")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "关键搜索词"),
            @ApiImplicitParam(name = "token", value = "请求token"),
            @ApiImplicitParam(name = "pageNum", value = "第N页"),
            @ApiImplicitParam(name = "pageSize", value = "每页N条"),
    })
    @GetMapping("/search/{keyword}/{pageNum}/{pageSize}")
    public Result searchArticleByKeyWord(@PathVariable("keyword") String keyword,
                                         @PathVariable("pageNum")  Integer pageNum,
                                         @PathVariable("pageSize")  Integer pageSize,
                                         HttpServletRequest request){
        String token = RequestUtils.getAccessToken(request);
        return elasticSearchService.searchArticleByKeyWord(keyword,token,pageNum,pageSize);
    }





    /**
     * 通过主键ID查询Article
     * @param id
     * @return
     */
    @ApiOperation("通过主键ID查询用Article")
    @ApiImplicitParam(name = "id", value = "ID", defaultValue = "0",required = true)
    @GetMapping("/{id}")
    public Result getArticleById(@PathVariable(value = "id",required = true) Long id){
        return articleCategoryTagsService.getArticle(id);
    }



    /**
     * 获取ArticleList
     * @return
     */
    @ApiOperation("获取ArticleList")
    @GetMapping("/list")
    public Result  getArticleList(){
        return articleCategoryTagsService.listArticle();
    }


    /**
     * 分页获取ArticleList
     * @return
     */
    @ApiOperation("分页获取ArticleList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "第N页"),
            @ApiImplicitParam(name = "pageSize", value = "每页N条")})
    @GetMapping("/list/{pageNum}/{pageSize}")
    public Result  getArticleList(@PathVariable("pageNum")   Long pageNum,
                               @PathVariable("pageSize") Long pageSize){
        return articleCategoryTagsService.listArticle(pageNum, pageSize);

    }


    /**
     * 分页获取ArticleList 通过tagsId
     * @return
     */
    @ApiOperation("分页获取ArticleList  通过tagsId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tagsId", value = "标签Id"),
            @ApiImplicitParam(name = "pageNum", value = "第N页"),
            @ApiImplicitParam(name = "pageSize", value = "每页N条")})
        @GetMapping("/listByTagsId/{tagsId}/{pageNum}/{pageSize}")
    public Result  getArticleListByTagsId(
            @PathVariable("tagsId")   Long tagsId,
            @PathVariable("pageNum")   Long pageNum,
            @PathVariable("pageSize") Long pageSize){
        return articleCategoryTagsService.listArticleByTagsId(tagsId,pageNum, pageSize);

    }


    /**
     * 分页获取ArticleList 通过categoryId
     * @return
     */
    @ApiOperation("分页获取ArticleList  通过categoryId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "分类Id"),
            @ApiImplicitParam(name = "pageNum", value = "第N页"),
            @ApiImplicitParam(name = "pageSize", value = "每页N条")})
        @GetMapping("/listByCategoryId/{categoryId}/{pageNum}/{pageSize}")
    public Result  getArticleListByCategoryId(
            @PathVariable("categoryId")   Long categoryId,
            @PathVariable("pageNum")   Long pageNum,
            @PathVariable("pageSize") Long pageSize){
        return articleCategoryTagsService.listArticleByCategoryId(categoryId,pageNum, pageSize);

    }



    /**
     * 获取ArticleList 多条件查询
     * @param searchMap
     * @return
     */
    @ApiOperation("获取ArticleList 多条件查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "第N页"),
            @ApiImplicitParam(name = "pageSize", value = "每页N条"),
            @ApiImplicitParam(name = "searchMap", value = "搜索条件",required = false)})
    @PostMapping("/list/many/{pageNum}/{pageSize}")
    public Result listArticle(@PathVariable("pageNum") Long pageNum,
                              @PathVariable("pageSize") Long pageSize,
                              @RequestBody Map<String, Object> searchMap){

        return articleCategoryTagsService.listArticle(pageNum,pageSize, searchMap);
    }





}
